package cn.neusoft.controller;

import cn.neusoft.pojo.Food;
import cn.neusoft.pojo.Food;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.service.IFoodService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {
	@Autowired
	private IFoodService foodService;
	@PostMapping      //  请求方式 + 请求路径   post  /Food
	public ResultBean save(@RequestBody Food food) throws Exception {
		System.out.println("----FoodController-----save--------");
		System.out.println(food);
		System.out.println("----------------------");
		if(food.getFoodId()==null) {
			return foodService.add(food);
		}
		return  foodService.update(food);
	}
//	@PutMapping
//	public ResultBean upddate(@RequestBody Food food) throws Exception {
//		System.out.println("----FoodController-----update--------");
//		return foodService.update(food);
//	}
	@DeleteMapping("/{id}")
	public ResultBean delete(@PathVariable("id") Integer id) throws Exception {
		System.out.println("----FoodController-----delete--------");
		return foodService.delete(id);
	}
	@GetMapping("/{id}")
	public ResultBean getById (@PathVariable("id") Integer id) throws Exception {
		System.out.println("----FoodController-----getById--------");
		return foodService.getById(id);
	}
	@GetMapping("/all")
	public ResultBean getAll ( ) throws Exception {
		System.out.println("----FoodController-----getAll--------");
		return foodService.getAll();
	}
	@PostMapping("/page")
	public ResultBean getByPage(@RequestBody FoodQuery foodQuery) throws  Exception {
		System.out.println("----FoodController-----getByPage--------");
		System.out.println(foodQuery);
		return  foodService.getByPage(foodQuery);
	}
	@GetMapping("/batch/{ids}")
	public ResultBean getBatchDel(@PathVariable("ids") String ids) throws Exception {
		//因为在前端的时候，直接变成19，21的形式了，在后端只需要进行转换即可
		System.out.println(ids);
		//将字符串进行分割
		String [] strs=ids.split(",");
		List<Integer> integerList=new ArrayList<>();
		for (String str:strs) {
			integerList.add(Integer.parseInt(str));
		}
		System.out.println(integerList);
		return foodService.batchDel(integerList);
	}
	@GetMapping("/business/{businessId}")
	public ResultBean getByBusinessId(@PathVariable("businessId") Integer id) {
		System.out.println("----FoodController-----getByBusinessId--------");
		return foodService.getBusinessId(id);
	}
}
